package cn.wolfcode.p2p.business.service.impl;
import cn.wolfcode.p2p.business.domain.StatisticsDayFlow;
import cn.wolfcode.p2p.business.mapper.ActiveWaterMapper;
import cn.wolfcode.p2p.business.mapper.StatisticsDayFlowMapper;
import cn.wolfcode.p2p.business.service.IActiveWaterService;
import cn.wolfcode.p2p.business.service.IStatisticsDayFlowService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.Date;
import java.util.Map;

@Service
@Slf4j
public class StatisticsDayFlowServiceImpl implements IStatisticsDayFlowService {
    @Autowired
    private StatisticsDayFlowMapper statisticsDayFlowMapper;

    @Autowired
    private IActiveWaterService activeWaterService;

    @Autowired
    private ActiveWaterMapper activeWaterMapper;

    @Override
    public void statisticsForDay() {
        //统计活跃流水
        Map<String, BigDecimal> statistics = activeWaterService.statistics();
        //保存至日流水
        StatisticsDayFlow statisticsDayFlow = new StatisticsDayFlow();
        statisticsDayFlow.setDate(new Date());
        statisticsDayFlow.setTotalIncome(statistics.get("income"));
        statisticsDayFlow.setTotalExpenses(statistics.get("expenses"));
        statisticsDayFlowMapper.insert(statisticsDayFlow);
        //截断活跃流水表
        activeWaterMapper.truncateTable();
        log.debug("统计一天流水");
    }

    @Override
    public Map<String,BigDecimal> statistics() {
        Map<String, BigDecimal> map = statisticsDayFlowMapper.queryTotalAmount();
        return map;
    }

}
